<?php
namespace App\Api;

use PhalApi\Api;
use PhalApi\Exception\BadRequestException;

class BaseController extends Api
{
    CONST SUCCESS_RETURN = 200;
    CONST AESKEY = '0x*0529&a00@5b7=6g3d0niff5b7d219';
    CONST RECHARGE = '1';                             //充值
    CONST TRANSFER = '2';                             //转账
    CONST MINERFEE = '3';                             //收取gas费
    CONST WHEEL    = '4';                             //大转盘活动
    CONST ELEVEN   = '5';                             //双十一活动
    CONST INTERNALTRANSFER = '1';                     //内部转账
    CONST EXTERNALTRANSFER = '2';                     //外部转账
    CONST SECRETKEY = '0x605297a0025b7b6bff5b7d219e78cd4f7c4dce928e86069d64c99a47547aa1e9';             //发币钱包私钥
    CONST GASADDRESS = '0xc875d24eeb806df5320d08294a8fc3d5facd4a4f';                //收取gas费公司钱包地址
    CONST GASFEE = '0.001';   //收取的gas费金额
    CONST OFFICIALADDRESS = '0xc875d24eeb806df5320d08294a8fc3d5facd4a4f';          //官方钱包地址
    CONST OFFICIALUSERID = '100014';                         //官方钱包uid
    CONST USERID = '22020';                              //系统uid

    private $hex_iv = '00000000000000000000000000000000';
    public function __construct()
    {
        if($this->get_params('sign'))
        {
            $this->key = hash('sha256', self::AESKEY, true);

            $param = $this->get_params('param');
            if($param)
            {
                $p = $this->get_params();
                $params = explode('-', $param);
                $combine = '';
                foreach($params as $v)
                {
                    $combine .= $v . $p[$v];
                }
                $encrypt = $this->encrypt($combine);
            }
            else
            {
                $encrypt = $this->encrypt($this->combine_params());
            }

            if(str_replace(' ', '+',$this->get_params('sign')) != $encrypt)
            {
                throw new BadRequestException('安全验证失败!');
            }
        }
    }

    public function get_params($param = '')
    {
        //var_dump($_SERVER['REQUEST_METHOD']);die;
        switch($_SERVER['REQUEST_METHOD'])
        {
            case 'GET'     :   $input = & $_GET;break;
            case 'POST'    :   $input = & $_POST;break;
            case 'PUT'     :   parse_str(file_get_contents('php://input'), $input);break;
            default:
                return NULL;
        }
        unset($input['s']);
        if($param)
            return @$input[$param];
        return $input;
    }

    /**
     * 组合参数
     */
    public function combine_params()
    {
        $combine_return = '';
        $params = $this->get_params();

        foreach($params as $k => $v)
        {
            if($k != 'sign')
                $combine_return .= $k.$v;
        }
        return $combine_return;
    }

    function hexToStr($hex)
    {
        $string='';
        for ($i=0; $i < strlen($hex)-1; $i+=2)
        {
            $string .= chr(hexdec($hex[$i].$hex[$i+1]));
        }
        return $string;
    }

    function encrypt($str) {
        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
        mcrypt_generic_init($td, $this->key, $this->hexToStr($this->hex_iv));
        $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
        $pad = $block - (strlen($str) % $block);
        $str .= str_repeat(chr($pad), $pad);
        $encrypted = mcrypt_generic($td, $str);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return base64_encode($encrypted);
    }

    public function decrypt($code,$key = 'Difdsfg&uYdhaha_g&d12!@#dsajadla*&^HU12891') {
        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
        mcrypt_generic_init($td, hash('sha256', $key, true), $this->hexToStr('00000000000000000000000000000000'));
        $str = mdecrypt_generic($td, base64_decode($code));
        $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return $this->strippadding($str);
    }
    private function strippadding($string) {
        $slast = ord(substr($string, -1));
        $slastc = chr($slast);
        $pcheck = substr($string, -$slast);
        if (preg_match("/$slastc{" . $slast . "}/", $string)) {
            $string = substr($string, 0, strlen($string) - $slast);
            return $string;
        } else {
            return false;
        }
    }
}